﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
 

using OptusMobile.Web.ServerComponents.BAL.HandsetManagement;
using OptusMobile.Web.ServerComponents.DAL;



namespace InsertImage
{
    public partial class AddImage : Form
    {
        HandsetManager hm = new HandsetManager();
        private SqlConnection con = new SqlConnection();
        private SqlCommand cmd = new SqlCommand();
        private SqlCommand cmdDetail = new SqlCommand();
       
        private DataSet ds = new DataSet();

        public AddImage()
        {
            con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream ms;
            byte[] picbyte;
            string strsql = "";
            string folders;
            int count = lvSecond.Items.Count;
            int num = lsimage.Items.Count;
            con.Open();


           
            for (int i = 0; i < count; i++)
            {
                folders = lvSecond.Items[i].SubItems[4].Text;
                if ((ms = File.OpenRead(folders)) != null)
                {
                    picbyte = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
                    
                    cmd.Connection=con;
                    cmd.CommandText="P_CreateID";
                    cmd.CommandType=CommandType.StoredProcedure;
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@tableName",SqlDbType.VarChar,5);
                    cmd.Parameters[0].Value = "HSIM";
                    cmd.Parameters.Add("@time", SqlDbType.Date);
                    cmd.Parameters[1].Value = DateTime.Today;
                    cmd.Parameters.Add("@BH", SqlDbType.VarChar, 30);
                    cmd.Parameters[2].Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    string newid=cmd.Parameters[2].Value.ToString();
                    strsql="insert into HandsetImage values (@imageID,@handsetID,@imageName,@imagePath,@fileContent,@fileType)";
                    cmd.CommandText=strsql;
                    cmd.CommandType=CommandType.Text;
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@imageID", SqlDbType.VarChar, 30).Value = newid;
                    cmd.Parameters.Add("@handsetID", SqlDbType.VarChar, 30).Value = lvSecond.Items[i].SubItems[0].Text;
                    cmd.Parameters.Add("@imageName", SqlDbType.VarChar, 50).Value = lvSecond.Items[i].SubItems[1].Text;
                    cmd.Parameters.Add("@imagePath", SqlDbType.VarChar, 100).Value = lvSecond.Items[i].SubItems[2].Text.Replace("\\", "/");
                    cmd.Parameters.Add("@fileContent", SqlDbType.Image).Value = picbyte;
                    cmd.Parameters.Add("@fileType", SqlDbType.VarChar, 20).Value = lvSecond.Items[i].SubItems[3].Text;

                    cmd.ExecuteNonQuery();
                    

                    ms.Close();

                    for (int j = 0; j < num; j++)
                    {
                        cmdDetail.Connection = con;
                        cmdDetail.CommandText = "P_CreateID";
                        cmdDetail.CommandType = CommandType.StoredProcedure;
                        cmdDetail.Parameters.Clear();
                        cmdDetail.Parameters.Add("@tableName", SqlDbType.VarChar, 5);
                        cmdDetail.Parameters[0].Value = "HIMD";
                        cmdDetail.Parameters.Add("@time", SqlDbType.Date);
                        cmdDetail.Parameters[1].Value = DateTime.Today;
                        cmdDetail.Parameters.Add("@BH", SqlDbType.VarChar, 30);
                        cmdDetail.Parameters[2].Direction = ParameterDirection.Output;
                        cmdDetail.ExecuteNonQuery();
                        string detailimageid = cmdDetail.Parameters[2].Value.ToString();
                        strsql = "insert into HandsetImageDetail values (@dimageID,@imageID,@imageName,@imagePath,@fileType)";
                        cmdDetail.CommandText = strsql;
                        cmdDetail.CommandType = CommandType.Text;
                        cmdDetail.Parameters.Clear();
                        cmdDetail.Parameters.Add("@dimageID", SqlDbType.VarChar, 30).Value = detailimageid;
                        cmdDetail.Parameters.Add("@imageID", SqlDbType.VarChar, 30).Value = newid;
                        cmdDetail.Parameters.Add("@imageName", SqlDbType.VarChar, 50).Value = lsimage.Items[j].SubItems[1].Text;
                        cmdDetail.Parameters.Add("@imagePath", SqlDbType.VarChar, 100).Value = lsimage.Items[j].SubItems[2].Text.Replace("\\", "/");
                        cmdDetail.Parameters.Add("@fileType", SqlDbType.VarChar, 20).Value = lsimage.Items[j].SubItems[3].Text;

                        cmdDetail.ExecuteNonQuery();
                    }


                }
            }

            
           
            con.Close();
            lsimage.Items.Clear();
            lvSecond.Items.Clear();
          
        }



        private void AddImage_Load(object sender, EventArgs e)
        {
            
            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "select handsetid from V_HandsetBasicInformation where handsetid not in(select handsetid from HandsetImage)";
            SqlDataAdapter adr = new SqlDataAdapter(cmd);
            adr.Fill(ds, "handset");
            int count= ds.Tables[0].Rows.Count;
            for (int i = 0; i < count; i++)
            {
                ListViewItem item = new ListViewItem(ds.Tables[0].Rows[i][0].ToString ());
                lshandset.Items.Add(item);
            }

            con.Close();

           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.* ";
            openFileDialog.ShowDialog();
            
            
        }

        private void openFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            int count = 0;
           
            if (openFileDialog.FileName != "")
            {
                count = openFileDialog.FileNames.Count();
                for (int i = 0; i < count; i++)
                {
                    int sindex = Path.GetFullPath(openFileDialog.FileNames[i]).LastIndexOf("\\Image");
                    ListViewItem item = new ListViewItem(lshandset.SelectedItems[0].Text);
                    item.SubItems.Add(Path.GetFileName(openFileDialog.FileNames[i]));
                    item.SubItems.Add(Path.GetFullPath(openFileDialog.FileNames[i]).Substring(sindex,15));
                    item.SubItems.Add(Path.GetExtension(openFileDialog.FileNames[i]));
                    item.SubItems.Add(Path.GetFullPath(openFileDialog.FileNames[i]));
                    lvSecond.Items.Add(item);
                    
                }
            }
            lshandset.SelectedItems[0].Remove();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileSecond.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.* ";
            openFileSecond.ShowDialog();
        }

        private void openFileSecond_FileOk(object sender, CancelEventArgs e)
        {
            int count = 0;

            if (openFileSecond.FileName != "")
            {
                count = openFileSecond.FileNames.Count();
                for (int i = 0; i < count; i++)
                {
                    int sindex = Path.GetFullPath(openFileSecond.FileNames[i]).LastIndexOf("\\Image");
                    ListViewItem item = new ListViewItem(lvSecond.SelectedItems[0].Text );
                    item.SubItems.Add(Path.GetFileName(openFileSecond.FileNames[i]));
                    item.SubItems.Add(Path.GetFullPath(openFileSecond.FileNames[i]).Substring(sindex, 21));
                    item.SubItems.Add(Path.GetExtension(openFileSecond.FileNames[i]));
                    item.SubItems.Add(Path.GetFullPath(openFileSecond.FileNames[i]));
                    lsimage.Items.Add(item);
                    
                }
            }
        }


    }
}
